package com.eoner.managerlibrary.user.request;

import com.eoner.managerlibrary.netsign.NetEnvManager;
import com.eoner.managerlibrary.netsign.version2.FCSignInterceptor;
import com.eoner.managerlibrary.netsign.version1.GetCookiesInterceptor;
import com.eoner.managerlibrary.netsign.version2.HttpInterceptorNoCheckManyRequest;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class UserDataRetrofit {

    private static UserDataApi api;

    public static UserDataApi getInstance() {
        if(api == null){
            synchronized (UserDataRetrofit.class){
                if(api == null){
                    new UserDataRetrofit();
                }
            }
        }
        return api;
    }

    private UserDataRetrofit() {
        //声明日志类
        HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor();
        //设定日志级别
        httpLoggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);//NONE
        //自定义OkHttpClient
        OkHttpClient.Builder okHttpClient = new OkHttpClient.Builder();
        okHttpClient.connectTimeout(30, TimeUnit.SECONDS);
        okHttpClient.readTimeout(30,TimeUnit.SECONDS);
        //添加拦截器
        okHttpClient.addInterceptor(new GetCookiesInterceptor());
        //添加签名
        okHttpClient.addInterceptor(new HttpInterceptorNoCheckManyRequest());
        //添加请求头
        okHttpClient.addInterceptor(new FCSignInterceptor());
        okHttpClient.addInterceptor(httpLoggingInterceptor);
        Retrofit ourInstance = new Retrofit.Builder()
                .baseUrl(NetEnvManager.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create()).client(okHttpClient.build()).build();
        api = ourInstance.create(UserDataApi.class);
    }

}
